package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Contract;
import com.example.service.ContractService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 合同管理
 */
@RestController
@RequestMapping("/contracts")
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping
    public Result selectContract(){
        List<Contract> contract = contractService.selectAllContract();
        String msg = contract!=null ? "" : "数据查询失败，请重试！";
        Integer code = contract != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,contract,msg);
    }
    @GetMapping("/{id}")
    public Result selectContract(@PathVariable Integer id){
        Contract contracts = null;

        String key = "Contract_ID：" +id;
        //先从redis中获取缓存数据
        contracts = (Contract) redisTemplate.opsForValue().get(key);

        if(contracts != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,contracts,"查询成功！");
        }
        contracts = contractService.selectContractById(id);
        redisTemplate.opsForValue().set(key,contracts,60, TimeUnit.MINUTES);

        String msg = contracts!=null ? "" : "数据查询失败，请重试！";
        Integer code = contracts != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,contracts,msg);
    }

    @PostMapping("/ByName")
    public Result selectContractByName(@RequestBody Contract contract){
        List<Contract> contract1 = contractService.selectContractByName(contract);
        String msg = contract1!=null ? "" : "数据查询失败，请重试！";
        Integer code = contract1 != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,contract1,msg);
    }

    @PostMapping
    public Result addContract(@RequestBody Contract contract){
        int i = contractService.addContract(contract);
        String msg = i > 0 ? "" : "数据添加失败，请重试！";
        if (i > 0) {
            System.out.println("插入成功");
            return new Result(Code.INSERT_OK,i,msg);
        } else {
            System.out.println("插入失败");
            return new Result(Code.INSERT_ERR,i,msg);
        }
    }
    @PutMapping
    public Result updateContract(@RequestBody Contract contract) {
        int i = contractService.updateContract(contract);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";

        //删除redis中缓存
        String key = "Contract_ID：" +contract.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteContractById(@PathVariable Integer id){
        int i = contractService.deleteContract(id);
        String msg = i > 0 ? "" : "数据删除失败，请重试！";

        //删除redis中缓存
        String key = "Contract_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败");
            return new Result(Code.DELETE_OK, i, msg);
        }
    }
    @GetMapping("/{page}/{size}")
    public Result findUserList(@PathVariable int page,@PathVariable int size) {
        PageInfo<Contract> page1 = contractService.findPage(page, size);
        Integer code = page1 != null ? Code.GET_OK : Code.GET_ERR;
        String msg = page1 != null ? "" : "分页失败，请重试！";
        return new Result(code, page1, msg);
    }
    @RequestMapping("/makeExcel")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "contract.xlsx", contractService.getOutputExcelProjectData());
    }
}
